// Mock数据统一导出文件
export * from './channels'
export * from './products'
export * from './qrcodes'
export * from './merchants'
export * from './service-providers'

// 统一的Mock API响应格式
export interface MockResponse<T = any> {
  code: number
  message: string
  data: T
  total?: number
  success: boolean
}

// 创建标准的Mock API响应
export const createMockResponse = <T>(data: T, total?: number): MockResponse<T> => {
  return {
    code: 200,
    message: 'success',
    data,
    total,
    success: true
  }
}

// 创建错误的Mock API响应
export const createMockErrorResponse = (message: string, code: number = 500): MockResponse<null> => {
  return {
    code,
    message,
    data: null,
    success: false
  }
}

// 模拟异步延迟
export const mockDelay = (ms: number = 300): Promise<void> => {
  return new Promise(resolve => setTimeout(resolve, ms))
}

// 分页参数接口
export interface PaginationParams {
  current: number
  pageSize: number
}

// 分页响应接口
export interface PaginatedResponse<T> {
  data: T[]
  total: number
  current: number
  pageSize: number
}

// 创建分页响应
export const createPaginatedResponse = <T>(
  data: T[],
  params: PaginationParams
): PaginatedResponse<T> => {
  const { current, pageSize } = params
  const startIndex = (current - 1) * pageSize
  const endIndex = startIndex + pageSize
  const pageData = data.slice(startIndex, endIndex)

  return {
    data: pageData,
    total: data.length,
    current,
    pageSize
  }
}

// 搜索过滤工具函数
export const filterByKeyword = <T>(data: T[], keyword: string, fields: (keyof T)[]): T[] => {
  if (!keyword) return data

  return data.filter(item => {
    return fields.some(field => {
      const value = item[field]
      return value && String(value).toLowerCase().includes(keyword.toLowerCase())
    })
  })
}

// 按字段过滤
export const filterByField = <T>(data: T[], field: keyof T, value: any): T[] => {
  if (value === undefined || value === null || value === '') return data

  return data.filter(item => item[field] === value)
}

// 状态过滤
export const filterByStatus = <T extends { status: number }>(data: T[], status?: number): T[] => {
  if (status === undefined || status === null) return data

  return data.filter(item => item.status === status)
}

// 日期范围过滤
export const filterByDateRange = <T>(
  data: T[],
  field: keyof T,
  startDate?: string,
  endDate?: string
): T[] => {
  if (!startDate && !endDate) return data

  return data.filter(item => {
    const itemDate = new Date(item[field] as string)
    const start = startDate ? new Date(startDate) : null
    const end = endDate ? new Date(endDate) : null

    if (start && itemDate < start) return false
    if (end && itemDate > end) return false

    return true
  })
}

// 排序工具函数
export const sortByField = <T>(data: T[], field: keyof T, order: 'asc' | 'desc' = 'asc'): T[] => {
  return [...data].sort((a, b) => {
    const aValue = a[field]
    const bValue = b[field]

    if (aValue < bValue) return order === 'asc' ? -1 : 1
    if (aValue > bValue) return order === 'asc' ? 1 : -1
    return 0
  })
}

// 生成随机ID
export const generateId = (prefix: string = 'ID'): string => {
  return `${prefix}${Date.now().toString().slice(-6)}${Math.random().toString(36).substr(2, 4)}`
}

// 格式化金额
export const formatAmount = (amount: number): string => {
  return new Intl.NumberFormat('zh-CN', {
    style: 'currency',
    currency: 'CNY',
    minimumFractionDigits: 2
  }).format(amount)
}

// 格式化费率
export const formatFeeRate = (rate: number): string => {
  return `${(rate * 100).toFixed(2)}%`
}

// 格式化时间
export const formatTime = (time: string): string => {
  return new Date(time).toLocaleString('zh-CN')
}

// 获取随机状态
export const getRandomStatus = (): number => {
  return Math.random() > 0.8 ? 0 : 1
}

// 获取随机日期
export const getRandomDate = (daysBack: number = 30): string => {
  const now = new Date()
  const randomDays = Math.floor(Math.random() * daysBack)
  const randomDate = new Date(now.getTime() - randomDays * 24 * 60 * 60 * 1000)
  return randomDate.toISOString().replace('T', ' ').substr(0, 19)
}

// 常用的模拟数据
export const mockData = {
  // 联系人姓名
  contactNames: ['张三', '李四', '王五', '赵六', '陈七', '刘八', '黄九', '周十'],

  // 联系电话
  contactPhones: ['13800138000', '13900139000', '13700137000', '13600136000', '13500135000'],

  // 邮箱域名
  emailDomains: ['@qq.com', '@163.com', '@gmail.com', '@hotmail.com', '@sina.com'],

  // 公司名称后缀
  companyTypes: ['有限公司', '股份有限公司', '科技有限公司', '贸易有限公司', '服务有限公司'],

  // 地区
  regions: ['北京', '上海', '广州', '深圳', '杭州', '成都', '武汉', '西安', '南京', '重庆'],

  // 行业类型
  industries: ['餐饮', '零售', '娱乐', '服务', '教育', '医疗', '金融', '物流', '制造', '科技']
}

// 生成随机联系人
export const generateRandomContact = () => {
  const name = mockData.contactNames[Math.floor(Math.random() * mockData.contactNames.length)]
  const phone = mockData.contactPhones[Math.floor(Math.random() * mockData.contactPhones.length)]
  const domain = mockData.emailDomains[Math.floor(Math.random() * mockData.emailDomains.length)]
  const email = `${name.toLowerCase()}${domain}`

  return { name, phone, email }
}

// 生成随机公司名称
export const generateRandomCompanyName = (): string => {
  const region = mockData.regions[Math.floor(Math.random() * mockData.regions.length)]
  const industry = mockData.industries[Math.floor(Math.random() * mockData.industries.length)]
  const companyType = mockData.companyTypes[Math.floor(Math.random() * mockData.companyTypes.length)]
  const number = Math.floor(Math.random() * 100) + 1

  return `${region}${industry}${number}${companyType}`
}